package com.atguigu.auth.controller;


import com.atguigu.auth.service.SysRoleService;
import com.atguigu.auth.service.SysUserRoleService;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.common.utils.MD5;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-06-01
 */
@Api(tags="用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService service;
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;

//    @ApiOperation(value = "来到我的界面，获取当前用户基本信息")
//    @GetMapping("getCurrentUser")
//    public Result getCurrentUser() {
//        Map<String,Object>map=sysUserService.getCurrentUser();
//        return Result.ok(map);
//    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        service.updateStatus(id, status);
        return Result.ok();
    }

    //查询所有角色 和当前用户所属角色
    @ApiOperation("获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable String userId){
        Long userId_long=Long.valueOf(userId);
        //findRoleDataByUserId会返回2个值，所有用map接收一下
        Map<String,Object> map=sysRoleService.findRoleDataByUserId(userId_long);
        return Result.ok(map);
    }

    // 为用户分配角色
    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
    //条件分页查询
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("用户条件分页")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo){
        Page<SysUser> pageParam = new Page<>(page,limit);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        //获取查询的条件值
        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        //判断条件是否为空并封装条件
        if(!StringUtils.isAllEmpty(username)){
            wrapper.like(SysUser::getUsername,username);
        }
        //ge  大于等于
        if(!StringUtils.isAllEmpty(createTimeBegin)){
            wrapper.ge(SysUser::getCreateTime,createTimeBegin);
        }
        //le 小于等于
        if(!StringUtils.isAllEmpty(createTimeEnd)){
            wrapper.le(SysUser::getCreateTime,createTimeEnd);
        }
        IPage<SysUser> pageModel = service.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }


    @ApiOperation("根据id查询用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        SysUser user = service.getById(id);
        return Result.ok(user);
    }

    @ApiOperation("保存用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser sysUser){
        //对密码进行加密，MD5加密
        String password = sysUser.getPassword();
        String passwordMd5 = MD5.encrypt(password);
        sysUser.setPassword(passwordMd5);

        service.save(sysUser);
        return Result.ok();
    }

    @ApiOperation("更新用户")
    @PutMapping("update")
    public Result update(@RequestBody SysUser sysUser){
        service.updateById(sysUser);
        return Result.ok();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable long id){
        service.removeById(id);
        return Result.ok();
    }





}

